
<script>
/* eslint-disable */

import Node from "./Node";
import AnimCCComponent from "./libs/Anim.cccomponent";

export default {
  name: "AnimationFrame",

  mixins: [Node],

  props: {
    spriteProps: {
      type: Object,
      default: () => {
        return {};
      },
    },
    animationProps: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    nodeProps: {
      handler(newv, oldv) {
        // console.log("sprite watch nodeProps",newv,oldv);
        let oldProps = this.curProps;
        this.curProps = this.mergeProps();
        this._updateNodeProps(this.ccnode, oldProps, this.curProps);
      },
    },
    nodeComps: {
      handler(newv, oldv) {
        // console.log("sprite watch nodeComps",newv,oldv);
        let oldComps = this.curComps;
        this.curComps = this.mergeComps();
        this._updateNodeComps(this.ccnode, oldComps, this.curComps);
      },
    },
    spriteProps: {
      handler(newv, oldv) {
        // console.log("sprite watch spriteProps",newv,oldv);
        let oldComps = this.curComps;
        this.curComps = this.mergeComps();
        this._updateNodeComps(this.ccnode, oldComps, this.curComps);
      },
    },
    animationProps: {
      handler(newv, oldv) {
        // console.log("sprite watch spriteProps",newv,oldv);
        let oldComps = this.curComps;
        this.curComps = this.mergeComps();
        this._updateNodeComps(this.ccnode, oldComps, this.curComps);
      },
    },
  },

  created() {
    // console.log("sprite created",this.spriteprops,this.ccnode);
    this.createFrame();
  },

  methods: {
    //////////////////////////
    // 下面函数替换Node
    createNode() {},
    updateNodeProps(node, oldp, newp) {},
    updateNodeComps(node, oldp, newp) {},

    //////////////////////////
    mergeProps() {
      return this.nodeProps;
      // return { ...this.nodeProps };
    },
    mergeComps() {
      let comps = [
        {
          type: cc.Sprite,
          props: {
            sizeMode: cc.Sprite.SizeMode.TRIMMED,
            ...this.spriteProps,
          },
        },
        {
          type: cc.Animation,
        },
        {
          type: AnimCCComponent,
          props: {
            ...this.animationProps,
          },
        },
      ];

      if (this.nodeComps) {
        this.nodeComps.forEach((c) => {
          if (c && c.type && c.type !== cc.Sprite && c.type !== cc.Animation) {
            comps.push(c);
          }
        });
      }

      return comps;
    },
    createFrame() {
      if (!this.ccnode) {

        this.curComps = this.mergeComps();
        this.curProps = this.mergeProps();
        this._createNode(this.curProps, this.curComps);
      }
    },
  },
};
</script>
